package com.supermap.dataservice.wanring;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dubbo.api.cloud.push.Notice;
import com.supermap.dubbo.api.cloud.push.PushService;
import com.supermap.dubbo.api.cloud.warnInfo.WarnInfo;
import com.supermap.dubbo.api.cloud.warnInfo.WarningInfoService;
import com.supermap.dubbo.api.disaster.DisasterTypeService;
import com.supermap.dubbo.api.warn.FileShare;
import com.supermap.dubbo.api.warn.WarnCap;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2020年04月20 9:20
 * @email 1301584033@qq.com
 */
@Controller
public class WarningTask {
        @Reference(timeout = 10000)
        private WarningInfoService warningInfoService;
        @Reference(timeout = 50000)
        private DisasterTypeService disasterTypeService;
        @Reference(timeout = 100000)
        private PushService pushService;
        /**
         * 多条预警信息入库加推送
         * */

        @Scheduled(cron = "0 0 * * * ?")
        public  void insertWarnInfoList(){
            String os = System.getProperty("os.name");
            System.out.println("推送数据"+"当前操作系统:"+os+"----------------------------------------------");
            if(os.toLowerCase().indexOf("windows")!=-1){
                System.out.println("判断为操作开发环境不推送预警信息，已退出！");
                return;
            }

            //初始化Json封装工具类
            //重庆的区域
            // List<Map<String,Object>> mapList = disasterTypeService.getAllWarnArea("50");
            //获取所有区域
            List<Map<String,Object>> mapList = disasterTypeService.getAllArea();

            for(Map<String,Object> map:mapList){
                String areaCode=(String)map.get("areaCode");
                String warnAllArea=(String)map.get("warnAreaCode");
                if(areaCode!=null&&warnAllArea!=null){
                    insertLists(areaCode,warnAllArea);
                }
            }

        }
        public void insertLists(String areaCode,String warnAllArea){
            //String noticetype=pushService.getProductNoticeTypes("quanguoapp2019131xsadwqeq");
            List<Notice> noticeList=new ArrayList<>();
            List<WarnInfo> warnInfoList=new ArrayList<>();
            try{
                ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
                FileShare client = (FileShare)context.getBean("client");
                List<WarnCap> customParseFileList = client.listWarnCapByElement(warnAllArea, "RED,BLUE,YELLOW,YELLOW,UNKOWN", "A", 10*6);
                //获取最近10分钟数据
                String dateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                if(customParseFileList!=null&&customParseFileList.size()>0){
                    for (WarnCap warnCap:customParseFileList){
                        String warnId=String.valueOf(new Date().getTime());
                        warnId=warnId.substring(0,9);
                        WarnInfo warnInfo =new WarnInfo();
                        warnInfo.setId(warnId);
                        warnInfo.setDescription(warnCap.getDescription());
                        warnInfo.setEffective(warnCap.getEffective().substring(0,19));
                        warnInfo.setEventType(warnCap.getEventType());
                        warnInfo.setEventTypeCN(warnCap.getEventTypeCN());
                        warnInfo.setExpires(warnCap.getExpires().substring(0,19));
                        warnInfo.setHeadline(warnCap.getHeadline());
                        warnInfo.setIdentifier(warnCap.getIdentifier());
                        warnInfo.setMsgType(warnCap.getMsgType());
                        warnInfo.setNote(warnCap.getNote());
                        warnInfo.setReferencesInfo(warnCap.getReferencesInfo());
                        warnInfo.setSender(warnCap.getSender());
                        warnInfo.setSeverity(warnCap.getSeverity());
                        warnInfo.setAreaCode(areaCode);
                        warnInfoList.add(warnInfo);
                        List<String> integerList= pushService.getAllUserIdByAreaCode(areaCode);
                        if(integerList!=null&&integerList.size()>0){
                            for(String userId:integerList){
                                Notice notice = new Notice();
                                notice.setUploadtime(dateTime);
                                notice.setNoticetype(7);
                                notice.setMessageid(Integer.valueOf(warnId));
                                notice.setNoticetitle(warnInfo.getHeadline());
                                notice.setReceiveuserid(userId);
                                notice.setSenduserid("332606");
                                noticeList.add(notice);
                            }
                        }

                    }
                }
                if(warnInfoList!=null&&warnInfoList.size()>0){
                    warningInfoService.insertWarnInfoList(warnInfoList);
//                if(noticeList!=null&&noticeList.size()>0) {
//                    pushService.putNoticeList(noticeList);
//                    //app总数推送
//                    for (Notice notice : noticeList) {
//                        MyWebSocket.sendInfo(pushService.getReceiveuserIdCount(String.valueOf(notice.getReceiveuserid()),noticetype).getJsonStr(), String.valueOf(notice.getReceiveuserid()));
//                        List<String> strings=pushService.getUserCode(notice.getReceiveuserid()+"");
//                        AppPush.UserCodePush(strings,"您收到一条预警信息",notice.getNoticetitle());
//                    }
//
//                }
                }

            }catch (Exception e){
                e.printStackTrace();

            }
        }
}
